﻿using System;
using System.Diagnostics;
using System.Threading;
using AMEE.TcpComm;

namespace TcpTest2
{
    class Program
    {
        static int Port = 5001;
        static TcpServer Server = new TcpServer(Port);
        static bool IsServerRunning;

        static TcpWriter Client;
        static bool IsClientRunning;

        static void Main(string[] args) {
            /* Create a listener that outputs to the console screen, and 
             * add it to the debug listeners. */
            TextWriterTraceListener myWriter = new TextWriterTraceListener(System.Console.Out);
            Debug.Listeners.Add(myWriter);

            Func<string, string> processor = clntMsg => {
                Thread.Sleep(1000);
                return "$:Done;";
            };

            ConsoleKey key;
            do {
                Program.printMenu();
                key = Console.ReadKey().Key;
                Console.WriteLine();
                switch (key) {
                    case ConsoleKey.S: // Server starten
                        if (Program.Server.IsRunning) {
                            Console.WriteLine("Server läuft bereits.\n");
                            break;
                        }
                        Program.Server.Start();
                        Program.IsServerRunning = true;
                        Console.WriteLine("Server gestartet.\n");
                        break;
                    case ConsoleKey.C:
                        if (Program.IsClientRunning) {
                            Console.WriteLine("Client läuft bereits.\n");
                        }
                        else {
                            Program.Client = new TcpWriter("127.0.0.1", Program.Port);
                            Program.Client.ResponseReceived += (sender, evtArg) => Console.WriteLine(evtArg.Response);
                            Thread writerThr = new Thread(() => Program.clientWrite());
                            Program.IsClientRunning = true;
                            writerThr.Start();
                        }
                        break;
                    case ConsoleKey.F1: // Server beenden
                        if (!Server.IsRunning) {
                            Console.WriteLine("Server lief gar nicht.\n");
                        }
                        else {
                            Program.Server.Stop();
                            IsServerRunning = false;
                            Console.WriteLine("Server heruntergefahren.\n");
                        }
                        break;
                    case ConsoleKey.F2: // Client beenden
                        if (!Program.IsClientRunning) {
                            Console.WriteLine("Client lief gar nicht.\n");
                        }
                        else {
                            Program.IsClientRunning = false;
                            Thread.Sleep(200);
                            Program.Client.Close();
                            Console.WriteLine("Client beendet.\n");
                        }
                        break;
                    default:
                        break;
                }
            } while (key != ConsoleKey.Escape);
            // Aufräumen
            if (Program.IsServerRunning) {
                IsServerRunning = false;
                Program.Server.Stop();
            }
            if (Program.IsClientRunning) {
                Program.IsClientRunning = false;
                Thread.Sleep(200);
                Program.Client.Close();
            }

            Console.WriteLine("\n...");
        }




        /// <summary>
        /// Thread Delegat. Sendet alle 2 Sekunden eine Testnachricht an den Server.<para>
        /// Beenden mit Program.IsClientRunning=false</para>
        /// </summary>
        static void clientWrite() {
            int count = 0;
            while (Program.IsClientRunning && Program.Client.Connected) {
                Program.Client.Send("Testnachricht #" + ++count);
                Thread.Sleep(5000);
            }
            Program.Client.Close();
        }



        static void printMenu() {
            Console.WriteLine("Drücke 'Escape' zum Beenden.");
            Console.WriteLine("Drücke 's' zum Starten eines Servers auf Port 5001");
            Console.WriteLine("Drücke 'c' zum Starten eines Clients (sendet alle 2 Sek. eine Nachricht).");
            Console.WriteLine("Drücke 'F1' zum Beenden des Servers.");
            Console.WriteLine("Drücke 'F2' zum Beenden des Clients.");
        }
    }
}
